local skel = fk.CreateSkill {
  name = "efengqi__jianxiong",
}

Fk:loadTranslationTable{
  ["efengqi__jianxiong"] = "奸雄",
  [":efengqi__jianxiong"] = "当你受到伤害后，你可以获得对你造成伤害的牌并摸一张牌。当你成为其他角色的普通锦囊牌的目标后，你可以将效果改为对你造成1点伤害。",

  ["#efengqi__jianxiong-get"] = "奸雄：你可以获得对你造成伤害的牌并摸一张牌",
  ["#efengqi__jianxiong-trick"] = "奸雄：你可以将%arg对你效果改为造成1点伤害",

  ["$efengqi__jianxiong1"] = "燕雀，安知鸿鹄之志！",
  ["$efengqi__jianxiong2"] = "夫英雄者，胸怀大志，腹有良谋！",
}

skel:addEffect(fk.Damaged, {
  anim_type = "masochism",
  audio_index = 1,
  can_trigger = function (self, event, target, player, data)
    return target == player and player:hasSkill(skel.name)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#efengqi__jianxiong-get" })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if data.card then
      local get = room:getSubcardsByRule(data.card, {Card.Processing})
      room:obtainCard(player, get, true, fk.ReasonJustMove, player, skel.name)
    end
    if not player.dead then
      player:drawCards(1, skel.name)
    end
  end,
})

skel:addEffect(fk.TargetConfirmed, {
  anim_type = "masochism",
  audio_index = 2,
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) then
      return data.from and data.from ~= player and data.card:isCommonTrick()
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, { skill_name = skel.name, prompt = "#efengqi__jianxiong-trick:::"..data.card:toLogString() })
  end,
  on_use = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.efengqi__jianxiongTos = data.extra_data.efengqi__jianxiongTos or {}
    table.insertIfNeed(data.extra_data.efengqi__jianxiongTos, player)
  end,
})


skel:addEffect(fk.PreCardEffect, {
  can_refresh = function(self, event, target, player, data)
    return data.to == player and
    data.extra_data and data.extra_data.efengqi__jianxiongTos and table.contains(data.extra_data.efengqi__jianxiongTos, data.to)
  end,
  on_refresh = function(self, event, target, player, data)
    data:changeCardSkill("efengqi__jianxiong_card_skill")
  end,
})

skel:addAI({
  think_skill_invoke = function(self, ai, skill_name, prompt)
    if type(prompt) ~= "string" then return false end
    local room = ai.room
    local player = ai.player
    if prompt:startsWith("#efengqi__jianxiong-get") then
      local data = ai.room.logic:getCurrentEvent().data ---@type DamageData
      local val = ai:getBenefitOfEvents(function(logic)
        logic:drawCards(player, 1)
      end)
      if data.card then
        local get = room:getSubcardsByRule(data.card, {Card.Processing})
        if #get > 0 then
          val = val + ai:getBenefitOfEvents(function(logic)
            logic:obtainCard(player, get, true, fk.ReasonJustMove)
          end)
        end
      end
      return val > 0
    elseif prompt:startsWith("#efengqi__jianxiong-trick") then
      local data = ai.room.logic:getCurrentEvent().data ---@type UseCardData
      local bad = ai:getBenefitOfEvents(function(logic)
        logic:damage{
          from = data.from,
          to = player,
          damage = 1,
        }
      end)
      -- AI不会计算安全血量，死也要卖
      if player.hp <= 1 then bad = 2 * bad end
      -- 此锦囊牌生效效果价值要减掉
      local trick_val = 0 --[[ ai:getBenefitOfEvents(function(logic)
        logic:doCardEffect{
          from = data.from,
          card = data.card,
          to = player,
        }
      end)
      --]]
      local val = ai:getBenefitOfEvents(function(logic)
        logic:drawCards(player, 1)
        logic:obtainCard(player, data.card, true, fk.ReasonJustMove)
      end)
      return (val - trick_val) > bad
    end
    return false
  end,
})


return skel
